#coding=utf-8
import os
import threading
import requests
import json
import datetime
def call_interface(urllist,reslist):
    """将本地图片上传到美图云
    Args:
        urllist: 外网可以访问的url列表文件
        reslist: 颜值识别的结果文件
    """
    # 接口url
    file_url=open(urllist,'r')
    lines=file_url.readlines()
    file_url.close()
    for line in lines:
        full_path=line.strip().split()[0]
        file_name=full_path.split('/')[-2]+'-'+full_path.split('/')[-1]
        commond='./mtshell_linux rput public '+file_name+' '+full_path+' http://stage.meitudata.com true'+' >>'+reslist
        os.system(commond)

def split_list(all_list,n):
    """划分文件为n份
    """
    file=open(all_list,'r')
    num_line=len(file.readlines())
    size=int(num_line/n)if num_line%n==0 else int(num_line/n)+1
    os.system('split -a 2 -d -l '+str(size)+' '+all_list+' list_')

def main():
    start=datetime.datetime.now()
    num_thread=30
    split_list('url.list',num_thread)
    file_list=[]
    thread_list=[]
    index=''
    for i in xrange(num_thread):
        if i<10:
            index='0'+str(i)
        else:
            index=str(i)
        file_list.append(('list_'+index,'list_res_'+index))
        thread_list.append('thread_'+index)

    for i in xrange(num_thread):
        thread_list[i] = threading.Thread(target = call_interface, args = (file_list[i][0],file_list[i][1]))
        thread_list[i].start()
    num_thread_alive=num_thread
    while num_thread_alive>0:
        num_thread_alive=num_thread
        for i in xrange(num_thread):
            if thread_list[i].is_alive()==False:
                num_thread_alive-=1
    if os.path.exists('./url_res.list'):
        os.system('rm url_res.list')
    os.system('cat list_res_* >>url_res.list && rm list_*')
    end=datetime.datetime.now()
    print end-start
if __name__ == '__main__':
    main()
